package main

import (
  "gorm.io/driver/mysql"
  "gorm.io/gorm"
)

const (
	mysql_user   = "root"
	mysql_passwd = "123456"
	mysql_ip     = "127.0.0.1"
	mysql_port   = "3306"
	mysql_qyDB   = "go_test"
)

type User2 struct {
  gorm.Model
  Name  string
  Email string
}

func main() {
  dsn := mysql_user+":"+mysql_passwd+"@tcp("+mysql_ip+":"+mysql_port+")/"+mysql_qyDB+"?charset=utf8"
  db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
  if err != nil {
    panic("failed to connect to database")
  }

  // 自动迁移数据库表结构
  err = db.AutoMigrate(&User2{})
  if err != nil {
    panic("failed to migrate database")
  }

  // 创建新用户记录
  user := User2{Name: "John Doe", Email: "john@example.com"}
  result := db.Create(&user)
  if result.Error != nil {
    panic("failed to create user")
  }
}
